<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * 用户菜单模型
 */
class UserMenu extends BaseModel
{
    use SoftDelete;

    /**
     * 类型
     */
    public const LINK = 'L';
    public const IFRAME = 'I';
    public const MENUS_LIST = 'M';

    protected array $allowModifyFields = ['status'];

    protected array $fields = ['id', 'id' => 'value', 'name' => 'label'];

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->query->order('sort', 'desc');
    }

    /**
     * 获取列表
     */
    public function getList(bool $onlyTrashed = false)
    {
        if (!isset($this->params['onlyMenu'])) {
            $this->query->where('type', '=', self::MENUS_LIST);
        }
        return parent::getList($onlyTrashed); // TODO: Change the autogenerated stub
    }

    /**
     * 新增
     * @return bool
     */
    public function add(): bool
    {
        $this->setParams($this->handleData($this->params));
        return parent::add(); // TODO: Change the autogenerated stub
    }

    public function edit($id): bool
    {
        $this->setParams($this->handleData($this->params));
        return parent::edit($id); // TODO: Change the autogenerated stub
    }

    /**
     * 处理数据
     * @param $data
     * @return array
     */
    public function handleData($data): array
    {
        if (empty($data['parent_id']) || $data['parent_id'] == 0) {
            $data['parent_id'] = 0;
        }
        return $data;
    }
}